﻿@model Webdiyer.WebControls.Mvc.PagedList<Sahit.iSoilHotel.Portal.ServiceProxy.Tavern.Models.Record.RecordListModel>
@{
    ViewBag.Title = "Index";
}
<script language="javascript" type="text/javascript" src="/Scripts/jquery-1.5.1.min.js"></script>
<script type="text/javascript" src="/Scripts/jquery.unobtrusive-ajax.min.js"></script>
<script language="javascript" type="text/javascript" src="/Scripts/MicrosoftAjax.js"></script>
<script language="javascript" type="text/javascript" src="/Scripts/MicrosoftMvcAjax.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        getRoomTypes();
        $("#searchButton").bind("click", function () {
            search();
        });
    });

    function getRoomTypes() {
        $.getJSON('@Url.Action("GetRoomType")', function (result) {
            var roomType = $("#roomTypeSelect");
            $.each(result, function (i, item) {
                roomType.append("<option value='" + item.ID + "'>" + item.Name + "</option>");
            });
        });
    }

    function search() {
        var roomTypeID = getRoomTypeIDFilter();
        var roomNumber = getRoomNumberFilter();
        var recordStatusIDs = getRecordStatusIDFilter();

        $('#divRecords').load(
        '@Url.Action("SearchRecord")',
        { 'recordStatusIDs': recordStatusIDs, 'roomTypeID': roomTypeID, 'roomNumber': roomNumber });
    }

    function getRoomTypeIDFilter() {
        return $("#roomTypeSelect").val();
    }

    function getRoomNumberFilter() {
        return $("#roomNumberInput").val();
    }

    function getRecordStatusIDFilter() {
        var recordStatusIDs = new Array();

        $('input[name="recordStatus"]').each(function (index, item) {
            if (item.checked == true) {
                recordStatusIDs.push($(item).val());
            }
        });
        return recordStatusIDs.join("~");
    }

    function checkin() {
        var selectedRecords = getSelectedRecords("reserved");
        if (isValidSelectedRecords(selectedRecords, "reserved")) {
            var checkinURL = $("#checkinURL").val();
            window.location = checkinURL + "?recordID=" + selectedRecords[0];
        }
    }

    function checkout() {
        var selectedRecords = getSelectedRecords("checkout");
        if (isValidSelectedRecords(selectedRecords, "checkout")) {
            var checkoutURL = $("#checkoutURL").val();
            window.location = checkoutURL + "?recordIs=" + selectedRecords[0];
        }
    }

    function isValidSelectedRecords(selectedRecords,filter) {
        if (selectedRecords.length == 0) {
            alert("Can not be null!");
            return false;
        }
        if (selectedRecords.length > 1) {
            alert("Can not be more than one!");
            return false;
        }
        return true;
    }

    function getSelectedRecords(filter)
    {
        var selectedRecords = new Array();
        $('#recordTable tbody input[name="record"]:checked').each(function () {
            var recordRow = $(this).closest("tr");
            if (recordRow.find('td[class="' + filter + '"]')) {
                selectedRecords.push(recordRow.attr("id"));
            }
        });
        return selectedRecords;
    }
</script>
<div>
    <div>
        <input type="checkbox" value="1" name="recordStatus" checked="checked"  />Reserved
        <input type="checkbox" value="2" name="recordStatus" checked="checked" />Checked
        In
        <input type="checkbox" value="3" name="recordStatus" />Checked Out
        <input id="roomNumberInput" type="text" />
        <select id="roomTypeSelect" title="Room Type">
            <option value="00000000-0000-0000-0000-000000000000">All</option>
        </select>
        <input id="searchButton" type="button" value="Search" />
    </div>
    <div id="divRecords">
        @{Html.RenderPartial("RecordList", Model); }
    </div>
    <div>
        <input type="button" value="Check In" onclick="checkin();" /><input type="hidden" id="checkinURL" value='@Url.Action("CheckIn")' />
        <input type="button" value="Check Out" onclick="checkout();" /><input type="hidden" id="checkoutURL" value='@Url.Action("CheckOut")' />
    </div>
</div>
